package blockr.function;

import blockr.model.RandomNumOBJ;
import blockr.util.Byte_XOR;

public class Decrypt_First_Block {
    public static byte[][][] Do_Decrypt_First_Block(byte[][][] All_Block, RandomNumOBJ firstBlockKey, int G, int arraySize) {
        byte[][] first_block = All_Block[0];
        byte[] R1 = firstBlockKey.getR1();
        byte[] R2 = firstBlockKey.getR2();
        int shift_x;
        int end;
        if (MappedBiggerFileReader.getBlock_index() == 0) {
            end = MappedBiggerFileReader.getPiece_index() - 1;
        } else {
            end = first_block.length;
        }
        for (int i = 0; i < end; i++) {
            byte[] index_piece = first_block[i];
            if (index_piece == null) {
                break;
            }
            byte[] index_random_num_1 = RandomNum.generate_index_random_num_1(R1, i % (arraySize * 8), arraySize);
            int temp = arraySize * 8 / 2;
            shift_x = ((G % temp) * (8 * i + G) + 19) % temp;   //(Gmod512&(file_num+G)+19)mod512
            byte[] index_random_num_2 = RandomNum.generate_index_random_num_2(R2, shift_x, arraySize);
            byte[] key = Byte_XOR.XOR(index_random_num_1, index_random_num_2);//key=R1i^R2j
            index_piece = Byte_XOR.XOR(index_piece, key);
            first_block[i] = index_piece;
        }
        All_Block[0] = first_block;
//        Output.output(first_block);
        return All_Block;
    }
}
